package com.ericcson.innerportalapp;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

public class AnnouncementsList extends ListActivity {
	// A reference to the local object
	private AnnouncementsList local;
	private NetworkInfo netInfo;
	private ConnectivityManager cm;	
	private ProgressDialog progress;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		progress = new ProgressDialog(this);
		cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		netInfo = cm.getActiveNetworkInfo();

		if (netInfo != null && netInfo.isConnectedOrConnecting()) 
		{

			// Set view
			setContentView(R.layout.activity_announcements_list);

			// Set reference to this activity
			local = this;

			GetRSSDataTask task = new GetRSSDataTask();
			try {
				// Start download RSS task
				task.execute("http://news.google.com/news?ned=us&topic=h&output=rss");

			} catch (Exception e) {
				Toast msga=Toast.makeText(getApplicationContext(), "Connection Error", Toast.LENGTH_SHORT);
				msga.setGravity(Gravity.BOTTOM, 0, 0);
				msga.show();
				this.finish();
			}

			ActionBar ab = getActionBar();
			ab.setDisplayHomeAsUpEnabled(true);
			ab.setDisplayShowCustomEnabled(true);
			ab.setTitle("Anouncements");
			ab.setIcon(R.drawable.ic_action_volume_on);
		}
		else
		{
			Toast msga=Toast.makeText(getApplicationContext(), "Please Check Your Connection", Toast.LENGTH_SHORT);
			msga.setGravity(Gravity.BOTTOM, 0, 0);
			msga.show();
			this.finish();
		}
	}

	private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
		@Override
		protected List<RssItem> doInBackground(String... urls) {

			// Debug the task thread name

			try {
				// Create RSS reader
				RssReader rssReader = new RssReader(urls[0]);

				// Parse RSS, get items
				return rssReader.getItems();

			} catch (Exception e) {
			}

			return null;
		}
		@SuppressLint("ResourceAsColor")
		@Override
		protected void onPreExecute() {

			super.onPreExecute();
			try {

				progress.show();
				progress.setContentView(R.layout.progressdialog);
				progress.setMessage("Connecting..");
				progress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog, int which) {
				        dialog.dismiss();
				        finish();
				    }
				});

			} catch (Exception e) {
				Log.d("Error Progress", e.getMessage());
				Toast msga=Toast.makeText(getApplicationContext(), "Connection Error", Toast.LENGTH_SHORT);
				msga.setGravity(Gravity.BOTTOM, 0, 0);
				msga.show();
				finish();
			}

		}

		@Override
		protected void onPostExecute(List<RssItem> result) {
			try {
				if (progress.isShowing()) 
				{
					progress.dismiss();
				}
				// Get a ListView from main view
				ListView itcItems = getListView();

				// Create a list adapter
				ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,R.layout.announ_items,R.id.anntext, result);
				// Set list adapter for the ListView
				itcItems.setAdapter(adapter);

				// Set list view item click listener
				itcItems.setOnItemClickListener(new ListListener(result, local));

			} catch (Exception e) {
				Toast msga=Toast.makeText(getApplicationContext(), "Connection Error", Toast.LENGTH_SHORT);
				msga.setGravity(Gravity.BOTTOM, 0, 0);
				msga.show();
				finish();
			}

		}
	}  
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (progress.isShowing()) 
		{
			progress.dismiss();
		}
		finish();
	}
	
}
